package main

import (
	"FlyBird/util"
	"github.com/hajimehoshi/ebiten"
	"image/png"
	"log"
	"os"
)


type Game struct{
	LocationX float64
	LocationY float64
	Bird string
	Ground string
	BirdState int     // 0 是结束  1是活着
}

func (g *Game) UpdateBird(screen *ebiten.Image)  {
	//  按空格 跳跃
	if ebiten.IsKeyPressed(ebiten.KeySpace) && g.BirdState != 0 && !isSpace && gameState == 1 &&!isstop {
		isSpace = true
		locationTemp = -5
		g.Bird = "imgs/up.png"
		util.JumpMusic()
	}

	//  松开空格
	if !ebiten.IsKeyPressed(ebiten.KeySpace) && gameState == 1{
		locationTemp = 2
		g.Bird = "imgs/down.png"
		isSpace = false
	}

	//  判断是否暂停，然后判断 小鸟此时 能否移动
	if !isstop { // 暂停
		g.LocationY = g.LocationY + float64(locationTemp)
	}

	// 碰到下边界  和  上边界
	if (g.LocationY >= 409 || g.LocationY <= -4 ){
		g.Bird = "imgs/die.png"
		g.BirdState = 0
	}
	//  死亡不让超出下边界
	if (g.LocationY >= 479){
		g.LocationY = 479;
	}

	//  小鸟死亡
	if (g.BirdState == 0){
		g.Bird = "imgs/die.png"
		g.LocationY +=3
		locationTemp = 3
	}

}

func (g *Game) DrawBird(screen *ebiten.Image){
	f, err := os.Open(g.Bird)
	if err != nil {
		log.Fatal(err)
	}
	img, err := png.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	filter := ebiten.FilterNearest

	// 把Image文件转成ebiten.Image文件，用于展示
	eImg, _ := ebiten.NewImageFromImage(img,filter)

	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(g.LocationX,g.LocationY)

	// 在屏幕上展示出图片
	screen.DrawImage(eImg, op)
}
